package com.example.test.conview.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by yue on 2016/4/28.
 */
@SuppressLint("AppCompatCustomView")
public class AddWordTextView extends TextView {

  // 边框颜色
  private int borderColor = Color.TRANSPARENT;

  // 文本颜色
  private int textColor = Color.WHITE;

  // 选中状态
  private boolean isStroke = false;

  public AddWordTextView(Context context) {
    super(context);
  }

  public AddWordTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public AddWordTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  // 获取选中状态
  public boolean isStroke() {
    return isStroke;
  }

  // 设置选中状态
  public void setStroke(boolean stroke) {
    isStroke = stroke;
  }

  /**
   * 设置控件的边框颜色、文本颜色、选中状态
   *
   * @param borderColor 边框颜色
   * @param textColor 文本颜色
   * @param isStroke 选中状态
   */
  public void setBorderColor(int borderColor, int textColor, boolean isStroke) {
    this.borderColor = borderColor;
    this.textColor = textColor;
    this.isStroke = isStroke;
    invalidate();
  }

  /**
   * 边框颜色
   *
   * @return 边框颜色id
   */
  public int getBorderColor() {
    return borderColor;
  }

  @Override
  protected void onDraw(Canvas canvas) {
    if (isStroke) {
      this.setTextColor(borderColor);
      TextPaint tp = this.getPaint();
      tp.setFakeBoldText(true);
      tp.setStrokeWidth(5);
      tp.setStyle(Paint.Style.STROKE);
      tp.setColor(borderColor);
      super.onDraw(canvas);

      tp.setColor(textColor);
      tp.setStyle(Paint.Style.STROKE);
      this.setTextColor(textColor);
      super.onDraw(canvas);
    } else {
      this.setTextColor(textColor);
      TextPaint tp = this.getPaint();
      tp.setFakeBoldText(false);
      tp.setStrokeWidth(0);
      super.onDraw(canvas);
    }
  }
}
